/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package bll;
import java.util.ArrayList;
import javax.swing.table.AbstractTableModel;
import object.Student;
/**
 *
 * @author Le Thai
 */
public class StudentModel extends AbstractTableModel{
    String [] columns={"Mã số","Họ đệm","Tên","Giới tính","Ngày sinh","Địa chỉ","Lớp"};
    ArrayList<Student> data=new ArrayList<>();
    
    public StudentModel(ArrayList<Student> data){
        this.data=data;
    }
       
    @Override
    public int getRowCount() {
        return data.size();
    }

    @Override
    public int getColumnCount() {
        return columns.length;
    }

    public String getColumnName(int columnIndex){
        return columns[columnIndex];
    }
    
    public Class<?> getColumnClass(int columnIndex){
        return getValueAt(0, columnIndex).getClass();
    }
        
    @Override
    public Object getValueAt(int rowIndex, int columnIndex) {
        if(data.isEmpty()) return "";//still return model even Null
        Object tmp=null;
        switch (columnIndex){
            case 0:
                tmp=data.get(rowIndex).getId();
                break;
            case 1:
                tmp=data.get(rowIndex).getLastName();
                break;
            case 2:
                tmp=data.get(rowIndex).getFirstName();
                break;
            case 3:
                tmp=data.get(rowIndex).isGender();
                break;
            case 4:
                tmp=data.get(rowIndex).getDayOfBirth();
                break;
            case 5:
                tmp=data.get(rowIndex).getHomeAddress();
                break;
            case 6:
                tmp=data.get(rowIndex).getCurentClass();
                break;
        }
        return tmp;        
    }
    //remove data row
    public void removeRowAt(int rowIndex){
        data.remove(rowIndex);
    }
    //add new row
    public void addRow(int index,String lastName,String firtName,boolean gender,String dob,String addr,String Class){
        Student std=new Student();
        std.setId(index);
        std.setFirstName(firtName);
        std.setLastName(lastName);
        std.setGender(gender);
        std.setDayOfBirth(dob);
        std.setHomeAddress(addr);
        std.setCurentClass(Class);
        data.add(std);
    }
}
